#include "exti.h"
#include <avr/interrupt.h>

using namespace hw;

static ExtiCallback exti_callback;


void Exti::start(ExtiCallback callback) {
    exti_callback = callback;
    // enable interrupt on PCINT18 aka PD2
    PCMSK2 |= (1 << PCINT18);
    // enable pin change interrupt 2
    PCICR |= (1 << PCIE2);
}


void Exti::cancel() {
    PCICR &= ~(1 << PCIE2);
    PCMSK2 &= ~(1 << PCINT18);
}

ISR(PCINT2_vect)
{
    bool value = PORTD & (1U << PD2);
    exti_callback(value);
}
